<?
    cria_variaveis($_POST,$_GET,$_FILES);
?>
<p class="titulo" align="center"><?php echo $msg->get('INVERSA_MSG_02') ?></font></p>
<p class="subtitulo" align="center"><?php echo $msg->get('INVERSA_MSG_03') ?></font></p>
<p>
<div class="tabbar">
    <ul>
    <li><a href="index.php?inc=inversa.php&modo=material"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material") echo " class=\"tabact\""; ?>><?php echo $msg->get('Material_Teorico') ?></a></li>
    <li><a href="index.php?inc=inversa.php&modo=exemplo"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") echo " class=\"tabact\""; ?>><?php echo $msg->get('Exemplos_Prontos') ?></a></li>
    <li><a href="index.php?inc=inversa.php&modo=teclado"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") echo " class=\"tabact\""; ?>><?php echo $msg->get('Entrada_Teclado') ?></a></li>
    </ul>
</div>
<div id="cas">
<?php
if (!isset($Calcular)) {
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material")
    {
        require_once("./classes/upload.php");
        $uploads = new Upload();
        $uploads->Lista("matriz");
    }
    else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") { //leitura de dados via banco de dados - modulo de exemplos e exercicios
        include "exibe_prob_matriz_inversa_xml.php";
	}
	else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") {
        if ((strlen($dim)==0) | ($dim<0))  { 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=inversa.php>";
            echo $msg->get('INVERSA_MSG_04');
            echo "<input type=text size='2' maxlength='2' name=dim value=".($dim).">";
            echo "<br><br>";
                echo "<input type=hidden name=executar value=sim>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_02').">";

            echo "</form>"; 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=inversa.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        }
        elseif (!checaNumerico($dim))
        {
    ?>
            <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
            <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
    <?php
        }
        else  {
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=inversa.php>";
		//echo INVERSA_MSG_04;
		echo "<input type=hidden name=dim value=".($dim).">";
		echo "<br><br>";
		echo "<p align=left><font face=Arial>".$msg->get('INVERSA_MSG_05')."</font>";
		echo "<nobr>";
		for ($i=0; $i<$dim; $i++) {
		   echo "<br><br>";
		   for ($j=0; $j<$dim; $j++) {
				echo "<input type=text name=A[".($i)."][".($j)."] maxlength=10 size=5 value=".($A[$i][$j]).">";
		   } //end forj
		} //end fori
		echo "</nobr>";
		echo "<p>";
		echo "<input class=button type=submit name=Calcular value=".$msg->get('G_MSG_04').">";
		echo "<br>";
		echo "</form>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=inversa.php>";
		echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
		echo "</form>";
	   } //end if
	   echo "</body>";
	}   
} else {
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo")
        verificaBib($xml_file);
    if (!checaNumerico($dim) || !checaNumerico($A))
    {
?>
        <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
        <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
<?php
    }
    else
    {
        echo "<br>";
        echo $msg->get('INVERSA_MSG_04');
        echo $dim;
        echo "<br><br>";
        echo "<p align=left><b>".$msg->get('INVERSA_MSG_05')."</b> <br>";
        echo "<table border=\"0\">";
        for ($i=0; $i<$dim; $i++) {
            echo "<tr>";
            for ($j=0; $j<$dim; $j++) {
                echo "<td width=\"50\" align=\"center\" valign=\"center\">".$A[$i][$j]."</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
        echo "<p>";
        
        //criando uma matriz extendida B, na qual A e a matriz a ser invertida, junto da matriz identidade
        for ($i=0; $i<$dim; $i++)
        {
            for ($j=0; $j<2*$dim; $j++)
            {
                if ($i==$j-$dim)
                {
                    $B[$i][$j]=1;
                }
                else
                {
                    if ($j<$dim)
                    {
                        $B[$i][$j]=$A[$i][$j];
                    }
                    else
                    {
                        $B[$i][$j]=0;
                    }
                }
            }
        }
    
        //transformando a matriz A em identidade, de modo que a identidade se torne a inversa de A
        for ($k=0;$k<$dim;$k++)
        {
            echo ($k+1)."&ordm; passo:<br>";
            echo "<table border=\"0\">";
            $p=$B[$k][$k];
            for ($i=0;$i<$dim;$i++)
            {
                echo "<tr>";
                for ($j=0; $j<$dim*2; $j++)
                {
                    echo "<td align=\"center\" valign=\"center\" width=\"50\">".round($B[$i][$j],4)."</td>";
                }
                echo "</tr>";
            }
            for ($i=0;$i<$dim;$i++)
            {
                $t[$i]=$B[$i][$k];
            }
            for ($j=$k;$j<2*$dim;$j++)
            {
                if ($p != 0)
                {
                    $B[$k][$j]=$B[$k][$j]/$p;
                    for ($i=0;$i<$dim;$i++)
                    {
                        if ($k!=$i)
                        {
                            $B[$i][$j]=$B[$i][$j]-$t[$i]*$B[$k][$j];
                        }
                    }
                }
            }
            echo "</table>";
        }
        echo "<p align=left><b>".$msg->get('INVERSA_MSG_07')."</b> <br>";
        echo "<table border=\"0\">";
        for ($i=0; $i<$dim; $i++) {
        echo "<tr>";
        for ($j=$dim; $j<2*$dim; $j++) {
            echo "<td align=\"center\" valign=\"center\" width=\"50\">".round($B[$i][$j],4)."</td>";
        }
        echo "</tr>";
        }
        echo "</table>";
        
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            $C[$i][$j]=0;
            for ($k=0;$k<$dim;$k++) {
                $C[$i][$j]=$C[$i][$j]+$A[$i][$k]*$B[$k][($j+$dim)];
            }
        }
        }
        echo "<br><br><b>".$msg->get('Verificacao_Resultado')."</b>";
        echo "\n\n<table border=\"0\">";
        for ($i=0;$i<=$dim;$i++)
        {
            echo "<tr>";
            for ($j=0;$j<$dim*3+2;$j++)
            {
                if ($i==0)
                {
                    if ($j == 0) echo "<td colspan=\"$dim\" align=\"center\" valign=\"center\">Matriz A</td><td>&nbsp;</td>";
                    if ($j == $dim+1) echo "<td colspan=\"$dim\" align=\"center\" valign=\"center\">Matriz A<sup>-1</sup></td><td>&nbsp;</td>";
                    if ($j == 2+2*$dim) echo "<td colspan=\"$dim\" align=\"center\" valign=\"center\">A * A<sup>-1</sup></td>";
                }
                else
                {
                    if ($j < $dim) echo "<td width=\"50\" align=\"center\" valign=\"center\">".$A[$i-1][$j]."</td>";
                    if ($j == $dim) echo "<td>&nbsp;</td>";
                    if ($j > $dim && $j < (1+(2*$dim))) echo "<td width=\"50\" align=\"center\" valign=\"center\">".round($B[$i-1][$j-1],4)."</td>";
                    if ($j == (1+(2*$dim))) echo "<td>&nbsp;</td>";
                    if ($j > (1+(2*$dim))) echo "<td width=\"50\" align=\"center\" valign=\"center\">".((round($C[$i-1][$j-$dim*2-2],4)>0)?round($C[$i-1][$j-$dim*2-2],4):-(round($C[$i-1][$j-$dim*2-2],4)))."</td>";
                }
            }
            echo "</tr>";
        }
        echo "</table>\n\n";
        
        echo "<p>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
            echo "<script language=\"javascript\" type=\"text/javascript\">\n";
            echo "function criarLinks(value) {\n";
            echo "document.exemplos.indice.value = value;\n";
            echo "document.exemplos.action = 'index.php?inc=inversa.php&modo=exemplo'\n";
            echo "document.exemplos.submit();\n";
            echo "}\n";
            echo "</script>";
            echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=\"hidden\" name=\"indice\">";
            echo "<br><br>".$msg->get('Outros_Exemplos')."<br>";
            $objeto = new DomDocument();
            $objeto->load($xml_file);
            $filho = $objeto->getelementsbytagname("matrices");
            if ($filho->length > 0)
            {
                $temp = $filho->item(0)->getelementsbytagname("matrix");
                $j = 0;
                for($i = 0; $i < $temp->length; $i++)
                {
                    $conteudo = $temp->item($i)->getelementsbytagname("dim");
                    if ($conteudo->length > 0)
                    {
                        if ($j != 0) echo " | ";
                        if ($j == $indice) echo "<b>";
                        echo "<a href=\"javascript:criarLinks($j)\" title=\"Dimens&atilde;o ".$conteudo->item(0)->nodeValue."\"><u>".($j+1)."</u></a>";
                        if ($j == $indice) echo "</b>";
                        $j++;
                    }
                }
            }
            echo "<br><Br>";
        }
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=inversa.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
    }
}
?>
</div>